import {
    Qiniu
} from './Setting';
import {
    Tips,
    Share,
    Map,
    Image,
    DateTime,
    Uploader,
    System,
    OAuth,
    Webview
} from './h5plus';

/**
 * WapApp 工具类
 *
 * @export
 * @class AppUtils
 */
export default class Utils {
    /**
     * 打开 模态框
     *
     * @static
     * @param {Ojbect} param
     *
     * @memberOf AppUtils
     */
    static openModal(param) {
        let view = Webview.getWebviewById(param.id);
        if (view == null) {
            if (param.transparent) {
                Webview.openTransparentWebview(param, {
                    bounce: "none"
                });
            } else if (param.navbar === false) {
                Webview.openNoTopBarWebview(param, {
                    bounce: "none"
                });
            } else {
                Webview.openWebview(param, {
                    bounce: "none"
                });
            }
        } else {
          view.show();
        }
    }

    /**
     * 打开对话框
     *   尺寸: big small
     *
     * @static
     * @param {any} param
     *
     * @memberOf Utils
     */
    static openMapModal(param) {
        const top = param.top === undefined ? '30%' : '50%';
        const opts = Object.assign({
            top: top
        }, param);
        let view = Webview.getWebviewById(param.id);
        if (view == null) {
            Webview.openTransparentWebview(opts, {
                left: '10px',
                right: '10px',
                bounce: "none"
            });
        } else {
          view.show();
        }
    }

    static openMapModalAsync(param) {
        const top = param.top === undefined ? '30%' : '50%';
        const opts = Object.assign({
            top: top
        }, param);
        return new Promise((resolve, reject) => {
            let view = Webview.getWebviewById(param.id);
            if (view == null) {
                view = Webview.openTransparentWebview(opts, {
                    left: '10px',
                    right: '10px',
                    bounce: "none"
                });
            } else {
              view.show();
            }
            resolve(view);
        });

    }

    /**
     * 获取指定页面的参数
     *
     * @static
     * @param {any} id
     * @returns
     *
     * @memberOf Utils
     */
    static getModalParams(id) {
        return id ? Webview.getWebviewById(id) : Webview.currentWebview();
    }

    static getMainWebview(){
      return Webview.getMainWebview();
    }

    /**
     * 获取首页窗口
     *
     * @static
     * @returns
     *
     * @memberOf Utils
     */
    static getLaunchWebview(){
      return Webview.getLaunchWebview();
    }


    /**
     * 关闭Webview窗口
     *
     * @static
     * @param {string} [id='']
     *
     * @memberOf Utils
     */
    static closeModal(id = '') {
        Webview.close(id);
    }

    /**
     * 预载网络页面
     *
     * @static
     * @param {any} urls
     *
     * @memberOf Utils
     */
    static prefetchURL(urls) {
        if (Object.prototype.toString.call(urls) === '[object Array]') {
            Webview.prefetchURLs(urls);
        } else if (Object.prototype.toString.call(urls) === '[object String]') {
            Webview.prefetchURL(urls);
        }
    }

    /**
     * 打开 分享
     *
     * @static
     * @param {string} [title='分享']
     * @param {string} [url='http://www.dcloud.io/']
     * @returns
     * @memberof Utils
     */
    static openShare(title = '分享', url = 'http://www.dcloud.io/') {
        return Share.openShare({
            title,
            url
        });
    }

    /**
     * 获取GPS定位
     *
     * @static
     * @param {string} [option={provider:'baidu'}]
     * @returns
     * @memberof Utils
     */
    static geolocation(option = {
        provider: 'baidu'
    }) {
        return Map.geolocation(option);
    }

    /**
     * 调用系统导航
     *
     * @static
     * @param {*} location
     * @param {*} toAddress
     * @memberof Utils
     */
    static openSysMap(location, toAddress, title = '') {
        // longitude = 116.347496;
        // latitude = 39.970191;
        // 设置目标位置坐标点和其实位置坐标点
        let src = new plus.maps.Point(location.longitude, location.latitude); // 天安门
        let dst = new plus.maps.Point(toAddress.longitude, toAddress.latitude); // 大钟寺
        // 调用系统地图显示
        plus.maps.openSysMap(dst, title, src);
    }

    /**
     * 打电话
     *
     * @static
     * @param {string} [moblie="10086"]
     * @param {boolean} [isConfirm=false]
     *
     * @memberOf AppUtils
     */
    static call(moblie = "10086", isConfirm = false) {
        System.dial(moblie, isConfirm);
    }

    /**
     * 调用此方法使得设备发出蜂鸣声。
     *
     * @static
     * @param {number} [times=3]
     *
     * @memberOf AppUtils
     */
    static beep(times = 3) {
        System.beep(times);
    }

    /**
     * 设备振动
     *
     * @static
     * @param {number} [milliseconds=1000]
     *
     * @memberOf AppUtils
     */
    static vibrate(milliseconds = 1000) {
        System.vibrate(milliseconds);
    }

    /**
     * 设备信息
     *
     * @static
     *
     * @memberOf AppUtils
     */
    static os() {
        return System.os();
    }

    /**
     * 打开 相机
     *
     * @static
     *
     * @memberOf AppUtils
     */
    static openCamera() {
        return Image.openCamera();
    }

    /**
     * 打开 照片
     *
     * @static
     *
     * @memberOf AppUtils
     */
    static openImage(option = {
        filter: "image",
        multiple: false
    }) {
        return Image.openImage(option);
    }

    /**
     * 压缩图片并保存到本地
     *
     * @static
     * @param {string} sourceSrc
     * @param {*} [option={}]
     * @memberof Utils
     */
    static compressImage(sourceSrc, option = {}) {
        return Image.compressImage(sourceSrc, option);
    }

    /**
     * 预览图片
     *
     * @static
     * @returns
     * @memberof Utils
     */
    static previewImage(items = []) {
        Image.previewImage(items);
    }

    /**
     * 获取登录授权认证并登录
     *
     * @static
     * @memberof AppUtils
     */
    static authLogin() {
        return OAuth.login();
    }

    /**
     * 注销登录授权认证
     *
     * @static
     * @memberof AppUtils
     */
    static authLogout() {
        return OAuth.logout();
    }

    /**
     * 打开日期 对话框
     *
     * @static
     * @returns
     * @memberof Utils
     */
    static openDate() {
        return DateTime.openDate();
    }

    /**
     * 打开时间 对话框
     *
     * @static
     * @returns
     * @memberof Utils
     */
    static openTime() {
        return DateTime.openTime();
    }

    /**
     * 文件上传
     *
     * @static
     * @param {string} file 上传的本地文件和唯一键
     * @param {array} params 上传的相关参数
     * @returns
     * @memberof Utils
     *
     * [七牛云 HTTP上传](https://developer.qiniu.com/kodo/manual/1272/form-upload)
     */
    static upload(file, params) {
        return Uploader.start(Qiniu.UPLOAD_URL, file, params);
    }

    /**
     * 获取APP本地文件
     *
     * @static
     * @param {string} [appFilePath='']
     * @memberof Utils
     */
    static getLocalFile(appFilePath = '') {
        return System.getLocalFile(appFilePath);
    }


   	static  settime(doc) {
    		let countdown=60;
		let time2 = function(doc,countdown){
		        if (countdown == 0) {
		            doc.removeAttribute("disabled");
		            let count_text ="免费获取验证码";

		            doc.value = count_text
		            doc.innerText= count_text
		            countdown = 60;
		        } else {
		            doc.setAttribute("disabled", true);
		           let count_text=  "重新发送(" + countdown + ")";

		            doc.value=count_text
		            doc.innerText= count_text
		            countdown--;
		            setTimeout(()=>{
		                time2(doc,countdown)
		            },1000)
		        }
		}
		time2(doc,countdown);
    }

   	/**
   	 *
   	 */
   	static time(){
   		 //获取当前时间
                var date = new Date();
                var now = date.getTime();
                //设置截止时间
                var endDate = new Date("2018-10-22 23:23:23");
                var end = endDate.getTime();
                //时间差
                var leftTime = end - now;
                //定义变量 d,h,m,s保存倒计时的时间
                if (leftTime >= 0) {
                    d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
                    h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
                    m = Math.floor(leftTime / 1000 / 60 % 60);
                    s = Math.floor(leftTime / 1000 % 60);
                }
   	}




}
